package socket.chat;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {

	public static void main(String[] args) {
		try {
			Socket socket=new Socket("192.168.1.115", 8088);//请求服务器建立连接
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					ObjectInputStream in;
					try {
						while(true){
							in=new ObjectInputStream(socket.getInputStream());
							Object o=in.readObject();
							System.out.println(o);
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}).start();
			Scanner kb=new Scanner(System.in); //键盘输入信息
			String s;   //声明字符串s
			ObjectOutputStream out=new ObjectOutputStream(socket.getOutputStream());
			//输入信息到socket
			do{
				s=kb.nextLine();  //s为键盘输入的内容
				out.writeObject(s);//输出键盘内容
				out.flush();//刷新
			} while(!s.equals("bye"));			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}
}